package birdsong.server;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class ServerJTest {

	//WE HAVE TO DO THE ASSERTS TO EVERY JTEST
	
	Server server;

	@Before
	public void Server(){
		server = new Server();
	}

	@Test (expected = NullPointerException.class)
	public void testStart() {
		//the start method should receive a serverCom, not yet implemented
		server.start(null);
	}


	@Test //(expected = NullPointerException.class) 
	public void testClientConnected() {

		server.clientConnected("KITOKEX");
		assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
		server.clientConnected("ENNES");
		assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
		server.clientConnected("ZE");
		assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());
		server.clientConnected("KITOKEX");
		assertTrue(" test if the client already exists in the list ",server.verifyClient("KITOKEX"));
		System.out.println(server.toString());
		
	}

	
	@Test
	public void testClientDisconnected() {

		server.clientConnected("KITOKEX");
		assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
		server.clientConnected("ENNES");
		assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
		server.clientConnected("ZE");
		assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());
		

		server.clientDisconnected("KITOKEX");
		assertNull("verify is the client is no longer on the list", server.getClient("KITOKEX", server.getClients()));
		server.clientDisconnected("KITOKEX");
		assertNull("verify is the client is no longer on the list", server.getClient("KITOKEX", server.getClients()));
		
	}

	@Test
	public void testGetClient() {
		server.clientConnected("KITOKEX");
		assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
		server.clientConnected("ENNES");
		assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
		server.clientConnected("ZE");
		assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());
		assertNotNull("verify if the client still exists", server.getClient("KITOKEX", server.getClients()));
		server.clientDisconnected("KITOKEX");
		assertNull("verify is the client is no longer on the list", server.getClient("KITOKEX", server.getClients()));
		
		
	}

	@Test
	public void testMessage() {

		
			server.clientConnected("KITOKEX");
			assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
			server.clientConnected("ENNES");
			assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
			server.clientConnected("ZE");
			assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());

			server.follow("KITOKEX", "ZE");
			server.follow("KITOKEX", "ENNES");


			
			server.message("ENNES", "teste 1");
			//need serverCom to test this method
		
			server.clientDisconnected("KITOKEX");
			assertNull("verify is the client is no longer on the list", server.getClient("KITOKEX", server.getClients()));
			
			server.message("KITOKEX", "ZEG");
			//need serverCom to test this method
	}


	@Test
	public void testFollow() {

		
			server.clientConnected("KITOKEX");
			assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
			server.clientConnected("ENNES");
			assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
			server.clientConnected("ZE");
			assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());
			
		server.follow("KITOKEX", "ZE");
		assertEquals("test if the client is on the follow list", "KITOKEX", server.getClient("KITOKEX",server.getClient("ZE", server.getClients()).getFollowers()).getNickName());

		server.follow("KITOKEX", "ENNES");
		assertEquals("test if the client is on the follow list", "KITOKEX", server.getClient("KITOKEX",server.getClient("ENNES", server.getClients()).getFollowers()).getNickName());

		server.follow("ENNES","KITOKEX");
		assertEquals("test if the client is on the follow list", "ENNES", server.getClient("ENNES",server.getClient("KITOKEX", server.getClients()).getFollowers()).getNickName());

		assertNull("test if the client is NOT on the follow list", server.getClient("ZE",server.getClient("ENNES", server.getClients()).getFollowers()));
		

		server.clientDisconnected("KITOKEX");
		
		 
		server.follow("ENNES", "KITOKEX");
		assertNull("test if the client is NOT on the follow list", server.getClient("KITOKEX",server.getClient("ENNES", server.getClients()).getFollowers()));


	}



	@Test
	public void testUnfollow() {
		server.clientConnected("KITOKEX");
		assertEquals(" test if the client is in the list ","KITOKEX", server.getClient("KITOKEX", server.getClients()).getNickName());
		server.clientConnected("ENNES");
		assertEquals(" test if the client is in the list ","ENNES", server.getClient("ENNES", server.getClients()).getNickName());
		server.clientConnected("ZE");
		assertEquals(" test if the client is in the list ","ZE", server.getClient("ZE", server.getClients()).getNickName());
		
	server.follow("KITOKEX", "ZE");
	assertEquals("test if the client is on the follow list", "KITOKEX", server.getClient("KITOKEX",server.getClient("ZE", server.getClients()).getFollowers()).getNickName());

	server.follow("KITOKEX", "ENNES");
	assertEquals("test if the client is on the follow list", "KITOKEX", server.getClient("KITOKEX",server.getClient("ENNES", server.getClients()).getFollowers()).getNickName());

	server.unfollow("KITOKEX", "ZE");
	assertNull("test if the client is NOT on the follow list", server.getClient("ZE",server.getClient("KITOKEX", server.getClients()).getFollowers()));
	
	}

	@Test
	public void testToString() {
		server.toString();
	}

}
